<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.1.0.js"></script>
<script type="text/javascript">
	function getComment(){ //모든댓글을 가져오는 함수 정의
		$.ajax({
			type:"post",
			url:"auth_getAllComment.do",
			data:"no=${requestScope.bvo.no}", //게시물의 번호를 전송
			dataType:"json",
			beforeSend:function(){
			},
			success:function(data){
				var sessionId = "${sessionScope.loginMember.id}";
				var sessionPoint = "${sessionScope.loginMember.point}";
				var form="<tr><td colspan=3></td></tr>";
				$.each(data,function(index,value){
					form+="<form>";
					form+="<tr><td colspan=2>";
					form+="<b>"+value.memberVO.id+"</b>";
					form+="</td>";
					form+="<td align=right>";
					form+=value.date;
					form+="</td></tr>";
					form+="<tr><input type='hidden' class='no' value='"+value.no+"'>";
					form+="<td colspan=2 class='textView'><pre>";
					form+=value.content;
					form+="</pre></td>";
					form+="<td align=right>";
					if(sessionId==value.memberVO.id){					
						form+="<a href='#' class='updateCommentBtn'>수정</a>";
						form+=" | ";
						form+="<a href='#' class='deleteCommentBtn'>삭제</a>";
					}else if(sessionPoint==-1){
						form+="<a href='#' class='deleteCommentBtn'>삭제</a>";
					}
					form+="</td>";
					form+="<tr style='border-bottom: 1px dashed silver;'><td colspan=3><br></td></tr>";
					form+="</form>";
				});
				$("#commentView").html(form); //댓글 영역에 표시한다
			}//callback
		});//ajax 
	}//getComment
	$(function(){ //페이지 시작시 함수 정의
		//alert("${requestScope.bvo.memberVO.id}");
		getComment(); //페이지 레디시 모든 댓글 호출
		$("#updateBoard").click(function(){ //게시글 수정 
			if(confirm("게시글을 수정하시겠습니까?")){
				location.href="${initParam.root}public/auth_updatePublicBoardInfo.do?no=${requestScope.bvo.no}";
			}//if
		});//updateBoard click
		$("#deleteBoard").click(function(){ //게시글 삭제
			if(confirm("게시글을 삭제하시겠습니까?")){
				location.href="${initParam.root}public/auth_deleteBoardByPubNo.do?no=${requestScope.bvo.no}";
			}//if
		});//updateBoard click
		$("#publicBoardList").click(function(){ //목록가기
			location.href="${initParam.root}public/getPublicBoardList.do";
		});//publicBoardList click
		
		$("#commentBtn").click(function(){ //댓글등록 버튼
			if($("#content").val()==""){
				alert("내용을 입력해주세요");
				return false;
			}
			 $.ajax({
					type:"post",
					url:"auth_writeComment.do",
					data:$("#commentForm").serialize(), //댓글정보를 전송
					dataType:"json",
					success:function(flag){
						if(!flag){
							alert("댓글등록 실패");
						}
						$("textarea[name=content]").val("");
						getComment();
					}//callback
			 });//ajax
		});//commentBtn click
		
		$("#fileListBtn").toggle(function(){ //버튼을 누르면 
			$("#fileView").show(); //숨겨진 첨부파일목록 영역을 보여주고 
		},function(){
			$("#fileView").hide(); //한번 더 누르면 감춘다. 
		});
		$("#fileView").hide(); //첨부파일목록 영역은 감춘다 (레디시점)
		$.ajax({ //페이지 레디시점에서 파일리스트를 호출한다.  
			type:"get",
			url:"auth_findOrgAndNewFileListByPubNo.do",
			data:"no=${requestScope.bvo.no}",
			dataType:"json",
			success:function(list){
				str="";
				$.each(list,function(index,value){
					str+="<a href=${initParam.root}public/auth_fileDownload.do?newfilename="+value.PUB_NEWFILENAME+"&orgfilename="+value.PUB_ORGFILENAME+">";
					str+=value.PUB_ORGFILENAME+"</a><br>";
				});
				$("#fileView").html(str);
			}//callback
	 });//ajax
		
		//댓글삭제버튼
		$("#commentView").on("click",".deleteCommentBtn",function(){
			if(confirm("댓글을 삭제하시겠습니까?")){
			 $.ajax({
					type:"get",
					url:"auth_deleteComment.do",
					data:"no="+$(this).parent().parent().find(".no").val(), //댓글정보를 전송
					dataType:"json",
					success:function(flag){
							getComment();
					}//callback
			 });//ajax
			}//confirm
		}); //commentView on click 
		$("#commentView").on("click",".updateCommentBtn",function(){
			 var str = "<textarea rows='2' cols='40' class='commentText' style='resize: none;'>"+$(this).parent().parent().find(".textView").text()+"</textarea>";
			str+="<input style='vertical-align: 96%; height: 37px;' type='button' value='수정완료' class='updateCommentOkBtn'>";
			$(this).parent().parent().find(".textView").html(str); 
		}); //commentView on click
		$("#commentView").on("click",".textView .updateCommentOkBtn",function(){
			     $.ajax({
				type:"get",
				url:"auth_updateComment.do",
				data:"no="+$(this).parent().parent().find(".no").val()+"&content="+$(this).parent().parent().find(".textView").find(".commentText").val(),
				//댓글정보를 전송
				dataType:"json",
				success:function(flag){
						getComment();
				} 
		 	});//ajax 
		});
		$("#replyWriteBtn").click(function(){
			if(confirm("답글을 작성하시겠습니까?")){
				location.href="${initParam.root}public/auth_replyBoardWriteInfo.do?no=${requestScope.bvo.no }";
			}
		});
		$("#updateLikeBtn").click(function(){
			$.ajax({
				type:"get",
				url:"auth_updateLike.do",
				data:"no=${requestScope.bvo.no}", 
				dataType:"json",
				success:function(flag){
						if(flag){
							alert("추천되었습니다!");
							location.reload(true);
						}else{
							alert("이미추천한 게시물입니다.");
						}
				}
		 	});//ajax  
		});//click
		
	});//ready
</script>
<table class="pub_detail_table" style="word-wrap: break-word; table-layout: fixed;">
	<thead>
		<tr id="thead">
			<td width="10%">${requestScope.bvo.no }</td>
			<td width="60%"><a href="${initParam.root}public/getPublicBoardList.do">정보공유</a> | <b style="font-size: 11pt;">${requestScope.bvo.title}</b></td>
			<td align="right" width="30%">${requestScope.bvo.date}</td>
		</tr>
		<tr>
			<td colspan="2">작성자 | <b style="font-size: 10pt;">${requestScope.bvo.memberVO.id}</b></td>
			<td align="right">조회수 ${requestScope.bvo.hit} | 추천수 ${requestScope.bvo.like}</td>
		</tr>
	</thead>
	<tbody>
		<tr id="fileBar">
			<td colspan="2">
				<pre>${requestScope.bvo.content}</pre>
			</td>
			<td valign="top" align="right" style="border-left: 1px dotted silver;">
				<a href="#" id="fileListBtn">첨부파일</a>
				<div id="fileView"></div>
			</td>
		</tr>
		<tr>
			<td colspan="2">
				<c:if test="${sessionScope.loginMember.id!=requestScope.bvo.memberVO.id}">
					<a href="#" id="updateLikeBtn"><img height="15" src="${initParam.root }img/like.jpg"> 추천</a>
				</c:if>
			</td>
			<c:choose>
			<c:when test="${sessionScope.loginMember.id==requestScope.bvo.memberVO.id}">
				<td align="right"><a href="#" id="updateBoard">수정</a> | <a href="#" id="deleteBoard">삭제</a></td>
			</c:when>
			<c:when test="${sessionScope.loginMember.point==-1}">
				<td align="right"><a href="#" id="deleteBoard">삭제</a></td>
			</c:when>
			<c:otherwise>
				<td></td>
			</c:otherwise>
			</c:choose>
		</tr>
		<tr><td colspan=3></td></tr>
	</tbody>
	<tfoot id="commentView">
	</tfoot>
</table>
<form id="commentForm" method="post">
	<textarea rows="2" cols="71" name="content" id="content" style="resize: none;"></textarea>
	<input type="hidden" name="memberVO.id" value="${sessionScope.loginMember.id }">
	<input type="hidden" name="boardNo" value="${requestScope.bvo.no}">
	<input style="vertical-align: 96%; height: 37px;" type="button" value="덧글입력" id="commentBtn">
</form>
<p align="right">
<input type="button" value="목록" id="publicBoardList">
<input type="button" value="답글" id="replyWriteBtn">
</p>

